//==========================================================================//
//                                                                          //
//  SERLCD Library              Version 0.1                     March 2012  //
//                                                                          //
//  Written for the Arduino Duemilanove w/ ATmega328                        //
//                                                                          //
//  SERLCD Arduino Library                                                  //
//                                                                          //
//  Created by JFLagarde (jeanfrancoislagarde@hotmail.com)                  //
//                                                                          //
//  This file is part of OpenWS (http://code.google.com/p/openws/)          //
//                                                                          //
//==========================================================================//
//                                                                          //
//  Notes:                                                                  //
//                                                                          //
//                                                                          //
//==========================================================================//
//                                                                          //
//  Copyright 2012 Jean-Francois Lagarde (jeanfrancoislagarde@hotmail.com)  //
//  This program is distributed under the terms of                          //
//  the GNU General Public License                                          //
//                                                                          //
//  This program is free software: you can redistribute it and/or modify    //
//  it under the terms of the GNU General Public License as published by    //
//  the Free Software Foundation, either version 3 of the License, or       //
//  (at your option) any later version.                                     //
//                                                                          //
//  This program is distributed in the hope that it will be useful,         //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of          //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           //
//  GNU General Public License for more details.                            //
//                                                                          //
//  You should have received a copy of the GNU General Public License       //
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                          //
//==========================================================================//

#include <SoftwareSerial.h>
#include <SERLCD.h>

SERLCD::SERLCD(int pin) : _serLCD(SoftwareSerial(0,pin)) {
	_pin = pin;
	_lcdDelay = 5;  // 10 is conservative, 2 actually works
	
	// Define the LCD parameters
	_serLCD = SoftwareSerial(0, _pin); // since the LCD does not send data back to the Arduino, we should only define the txPin
}

void SERLCD::init() {
	// Initialize the LCD screen
	pinMode(_pin, OUTPUT);
	_serLCD.begin(9600);
	delay(_lcdDelay);
	clear();
	goTo(0,0);
}
void SERLCD::goTo(int row, int col) {
	_serLCD.write(0xFE);   //command flag
	_serLCD.write(col + row*64 + 128);    //position 
	delay(_lcdDelay);
}
void SERLCD::clear(){
	_serLCD.write(0xFE);   //command flag
	_serLCD.write(0x01);   //clear command.
	delay(_lcdDelay);
}
void SERLCD::print(char* text) {
	_serLCD.print(text);
}
void SERLCD::printData(int dataLine1_x10, char* unitLine1, int dataLine2_x10, char* unitLine2) {
	clear();

	goTo(0,0);
	_serLCD.print(dataLine1_x10/10);
	_serLCD.print(".");
	_serLCD.print(dataLine1_x10%10);
	_serLCD.print(" ");
	_serLCD.print(unitLine1);

	goTo(1,0);
	_serLCD.print(dataLine2_x10/10);
	_serLCD.print(".");
	_serLCD.print(dataLine2_x10%10);
	_serLCD.print(" ");
	_serLCD.print(unitLine2);
}

//PRIVATE methods